<template>
  <div class="infoList">
    <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
      <el-form-item>
        <el-button type="primary" @click="addStudent()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center"> </el-table-column>
      <el-table-column prop="sex_text" label="性别" align="center"> </el-table-column>
      <el-table-column prop="age" label="年龄" align="center"> </el-table-column>
      <el-table-column prop="farther" label="父亲" align="center"> </el-table-column>
      <el-table-column prop="mother" label="母亲" align="center"> </el-table-column>
      <el-table-column prop="time" label="入校时间" align="center"> </el-table-column>
      <el-table-column prop="address" label="地址" align="center"> </el-table-column>
      <el-table-column prop="phone" label="联系方式" align="center"> </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope" class="edits">
          <el-button type="danger" size="mini" style="width=45%" icon="el-icon-delete" @click="del(scope.row)">删除</el-button>
          <el-button type="danger" size="mini" style="width=45%" icon="el-icon-edit" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="state ? '新增学生信息' : '修改学生信息'" class="adds" :rules="rules" :visible.sync="dialogFormVisible" width="500px">
      <el-form class="Sname" :model="form" ref="form">
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
          <el-input v-model="form.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父亲" :label-width="formLabelWidth" prop="farther">
          <el-input v-model="form.farther" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="母亲" :label-width="formLabelWidth" prop="mother">
          <el-input v-model="form.mother" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="入校时间" :label-width="formLabelWidth" prop="time">
          <el-date-picker v-model="form.time" formate="yyyy 年 MM 月 dd 日" value-formate="yyyy-MM-dd" type="date" placeholder="选择日期"> </el-date-picker>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth" prop="adress">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWidth" prop="phone">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sure('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { info, infos, delinfo } from '../../Api/api'
export default {
  data() {
    return {
      tableData: [],
      formInline: {
        name: '',
      },
      state: true,
      total: 0,
      rules: {
        name: [{ required: true, message: '请输入姓名' }],
        sex: { required: true },
        age: [{ required: true, message: '请输入年龄' }],
        time: [{ required: true, message: '请输入时间' }],
        address: [{ required: true, message: '请输入地址' }],
        phone: [{ required: true, message: '请输入手机号' }],
      },
      form: {
        name: '',
        sex: 1,
        age: '',
        farther: '',
        mother: '',
        time: '',
        address: '',
        phone: '',
      },
      dialogFormVisible: false,
      formLabelWidth: '70px',
    }
  },
  created() {
    this.getData()
  },
  methods: {
    del(row) {
      this.$alert('你确定要删除嘛', '提示', {
        confirmButtonText: '确定',
        callback: () => {
          delinfo(row.id).then((res) => {
            if (res.data.status == 200) {
              this.getData()
              this.$message({ message: res.data.message, type: 'success' })
            }
          })
        },
      })
    },
    edit(row) {
      this.state = false
      // 进行一次拓展，不然的话对话框数据变化底层也会发生变化
      this.form = { ...row }
      this.dialogFormVisible = true
    },
    getData() {
      infos().then((res) => {
        console.log('res', res)
        if (res.data.status == 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    addStudent() {
      this.state = true
      this.dialogFormVisible = true
      // this.form = ''
    },
    sure(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          if (this.state) {
            info('post', this.form).then((res) => {
              if (res.data.status == 200) {
                this.getData()
                this.dialogFormVisible = false
                this.$message({ type: 'success', message: res.data.message })
              }
            })
          } else {
            info('put', this.form).then((res) => {
              if (res.data.status == 200) {
                this.getData()
                // 校验的接口禁用标识符
                this.$refs[form].resetFields()
                this.dialogFormVisible = false
                this.$message({ type: 'success', message: res.data.message })
              }
            })
          }
        }
      })
    },
  },
}
</script>

<style lang="scss">
.infoList {
  .el-pagination {
    align-items: center;
    margin-top: 20px;
  }
  .adds {
    text-align: center;
    font-size: 30px;
    .edits {
      display: flex;
    }
  }
}
</style>
